/*
* Copyright (c) 2008 Matt Giger http://earthbrowser.com/
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
* ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

/**
* TimeControl
*
* Control the camera zoom with a graphical slider with buttons
* 
*/
function TimeCtl(plugin, properties)
{
	// Get into the plugin namespace
	with(plugin)
	try
	{
		// Get a reference to the camera for later
		var clock = getClock();
		
		// Containing Pane
		var base = new Pane();
		
		// Add user specialized properties
		if(properties != undefined)
			base.setProperties(properties);
		
		// Add a Slider control
		var slider = base.addPane(new Slider({ invert:false, vertical:false }));
		
		// Set the slider back
		var back = slider.setBack(new Frame({height:22, width:180, frameRadius:10, frameWidth:1, backColor:0xffffff, backAlpha:0.7}));

		// Set the Slider thumb
//		var thumb = slider.setThumb(new Frame({x:10, y:2, height:18, width:7, frameRadius:4, frameWidth:1, backColor:0, backAlpha:0.5}));
		var thumb = slider.setThumb(new Icon({y:2, skin:"time_arrow"}));

		// Play button
		var play = base.addPane(new Icon({skin:"play", x:182, y:3, binding:BIND_TOP|BIND_RIGHT}));
		play.addListener("onclick", function(e) {
			clock.setAnimating(true);
		});

		// Pause button
		var pause = base.addPane(new Icon({skin:"pause", x:182, y:3, visible:false, binding:BIND_TOP|BIND_RIGHT}));
		pause.addListener("onclick", function(e) {
			clock.setAnimating(false);
		});
		
		var isAnimating = clock.getAnimating();
		pause.setVisible(isAnimating);
		play.setVisible(!isAnimating);
		
		// Current time text
		var timeText = base.addPane(new Label({x:4, y:2, color:0, text:"", binding:BIND_LEFT|BIND_RIGHT|BIND_TOP}));
	
		slider.addListener("mouseover", function(e) {
			back.setBackColor(0xffeeee);
			thumb.setFrameColor(0xffffff);
		});
		
		slider.addListener("mouseout", function(e) {
			back.setBackColor(0xffffff);
			thumb.setFrameColor(0);
		});
		
		// Listen for the clock to change the slider value
		clock.addListener("onchange", function(e) {
			slider.setValue(clock.getTimePercentage());
			timeText.setText(clock.getTime().localeString());
		});
		
		// Set the play/pause buttons from our current state
		clock.addListener("onstatechange", function(e) {
			var isAnimating = plugin.getClock().getAnimating();
			pause.setVisible(isAnimating);
			play.setVisible(!isAnimating);
		});
		
		// listen for the slider value to change the zoom level
		slider.addListener("onslider", function(e) {
			clock.setAnimating(false);
			clock.setClockMode(false);
			clock.setTimePercentage(slider.getValue());
			timeText.setText(clock.getTime().localeString());
		});
	}
	catch(err)
	{
		alert(err.toString());
	}
	
	return base;
}
